import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class CalendarTest {
    public static void main(String[] args) {
        String[] week = {"日", "一", "二", "三", "四", "五", "六"};
        Scanner input = new Scanner(System.in);
        int year, month, begin = 1;
        int cnt;
        Calendar calendar;
        int dayOfMonth;
        int start;
        String s;
        boolean isFirstLine;

        while (true) {
            System.out.print("请分别输入年份和月份: ");
            year = input.nextInt();
            month = input.nextInt();

            calendar = new GregorianCalendar(year, month - 1, begin);
            dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取月份的天数
            start = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 求得该月份的第一天是星期几

            isFirstLine = true;
            cnt = 0;

            for (String str : week)
                System.out.print(str + "　 ");
            System.out.println();

            if(start != 0) {
                for (int i = 1; i <= start; ++i)
                    System.out.print("    ");
            }

            for (int i = 1; i <= dayOfMonth; ++i) {
                s = String.format("%2d", i);
                System.out.print(s + "  ");

                if (!isFirstLine) {
                    ++cnt;
                    if (cnt % 7 == 0)
                        System.out.println();
                }
                if (i == 7 - start) {
                    System.out.println();
                    isFirstLine = false;
                }

            }
            System.out.println();
        }
    }
}
